home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: HELP: Floating point precision
- Date: Sat, 16 Mar 96 00:25:57 GMT
- Organization: none
- Message-ID: <826935957snz@genesis.demon.co.uk>
- References: <3140831B.1597@interport.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: relay-4.mail.demon.net!post.demon.co.uk!genesis.demon.co.uk
-
- In article <3140831B.1597@interport.com> jeremy@interport.com "jeremy" writes:
-
- >The mechanism for casting 4-byte float to 8-byte double is
- >causing me some problems. The following code:
- >
- > float f;
- > double d;
- > f = 1.332;
- > d = (double) f;
- >
- >results in:
- >
- >f = 1.33200
- >d = 1.3320000171661
- >
- >(I am using Microsoft Visual C++ 2.1 compiler).
- >
- >I understand that this is due to complexities in
- >floating-point storage beyond my ken. Is there any run-time
- >library function which would cast f to 1.3320000000000 ? If not,
- >does anyone know how to write such a function? Using a lot of
- >processor cycles is not a big issue in the place I am having
- >this problem; I could afford to waste a little time.
-
- You could start with something like:
-
- d = floor(f * x + 0.5) / x;
-
- where x is a double quantity that is a power of 10 and determines the number
- of decimal places you want to keep (e.g. 1000.0 here for 3dp or 100000.0 for
- 5 dp). If f is negative you would need to use ceil() instead of floor().
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-